package com.frogs.ucenter.controller;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.UserOpenPlatformInfo;
import com.frogs.ucenter.service.UserOpenPlatformInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin/userOpenPlatformInfo")
public class UserOpenPlatformInfoController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/userOpenPlatformInfo";

    private static final String HOME_VIEW = "admin/userOpenPlatformInfo/home";

    private static final String EDIT_VIEW = "admin/userOpenPlatformInfo/edit";

    @Autowired
    private UserOpenPlatformInfoService userOpenPlatformInfoService;

    @RequestMapping()
    public String listUserOpenPlatformInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserOpenPlatformInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",userOpenPlatformInfoService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveUserOpenPlatformInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserOpenPlatformInfo userOpenPlatformInfo) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
            userOpenPlatformInfoService.insert( userOpenPlatformInfo ); 
            builder = DWZResponse.getSucessBuilder("success");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showUserOpenPlatformInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( userOpenPlatformInfoService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateUserOpenPlatformInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserOpenPlatformInfo userOpenPlatformInfoNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                UserOpenPlatformInfo userOpenPlatformInfo = userOpenPlatformInfoService.getById( id ); 
                PropertyUtils.copyProperties(userOpenPlatformInfo, userOpenPlatformInfoNew);
                userOpenPlatformInfoService.update( userOpenPlatformInfo ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("success");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteUserOpenPlatformInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            userOpenPlatformInfoService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("删除失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    private PageList<UserOpenPlatformInfo> getPageList(ModelMap model) {
        PageList<UserOpenPlatformInfo> userOpenPlatformInfoList = userOpenPlatformInfoService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userOpenPlatformInfoList",userOpenPlatformInfoList);
        model.put("pageTurn",userOpenPlatformInfoList.getPageTurn());
        return userOpenPlatformInfoList;
    }
}